﻿using System.Collections.Generic;
using System.ServiceModel.Activation;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using MusicWorks.Service.DataContracts;
using MusicWorks.Service.Helpers;
using MusicWorks.Service.Contracts;

namespace MusicWorks.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ProfileService : BaseServiceClass,IProfileService
    {
   

   
        public UserProfile GetUser(string userId, bool addressesNeeded)
        {
            if (!string.IsNullOrEmpty(userId))
            {
                var query = new CommerceQuery<UserProfile>();
                var userProfileProperties = new CommercePropertyCollection();
                userProfileProperties.Add(CommerceEntity.PropertyName.Id);
                userProfileProperties.Add(UserProfile.PropertyName.FirstName);
                userProfileProperties.Add(UserProfile.PropertyName.LastName);
                userProfileProperties.Add(CommerceEntity.PropertyName.DateModified);
                query.Model.Properties = userProfileProperties;
                query.SearchCriteria.Model.Id = userId;

                // Create related operations to populate relationships      
                // Populate relationships
                if (addressesNeeded)
                {
                    var addressProperties = new CommercePropertyCollection();
                    addressProperties.Add(Address.PropertyName.Line1);
                    addressProperties.Add(Address.PropertyName.Line2);
                    
                    // Add request to populate address list.
                    var queryAddress = new CommerceQueryRelatedItem<Address>(UserProfile.RelationshipName.Addresses);
                    queryAddress.Model.Properties = addressProperties;
                    query.RelatedOperations.Add(queryAddress);

                    // Add request to populate preferred address.
                    var queryPreferredAddress =
                        new CommerceQueryRelatedItem<Address>(UserProfile.RelationshipName.PreferredAddress);
                    queryPreferredAddress.Model.Properties = addressProperties;
                    query.RelatedOperations.Add(queryPreferredAddress);
                }


                // Process the request.
                CommerceResponse response = Broker.ProcessRequest(query.ToRequest());
                var queryResponse = response.OperationResponses[0] as CommerceQueryOperationResponse;
                if (queryResponse != null)
                    if (queryResponse.CommerceEntities.Count > 0 &&
                        queryResponse.CommerceEntities[0] != null)
                    {
                        return queryResponse.CommerceEntities[0];
                    }
            }

            return null;
        }
    }
}
